package com.lczc.soa.filter;

import com.alibaba.fastjson.JSONObject;
import com.lczc.soa.domain.Admin;
import com.lczc.soa.domain.Syslog;
import com.lczc.soa.domain.User;
import com.lczc.soa.mapper.SyslogMapper;
import com.lczc.soa.utils.IPUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.Set;

/**
 * Created by Janus on 2019/8/3.
 */
public class SyslogFilter implements Filter {
    private final static Logger log = LoggerFactory.getLogger(SyslogFilter.class);

    @Autowired
    private SyslogMapper logMapper;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.debug("SyslogFilter init..."+filterConfig);
        String exclusions = filterConfig.getInitParameter("exclusions");
        System.out.println("\n-------- SyslogFilter.init --- ");
        System.out.println(exclusions);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
        Map<String, String[]> parameterMap = httpRequest.getParameterMap();
        JSONObject paramObject = new JSONObject();
        Set<String> keySet = parameterMap.keySet();
        for(String key:keySet){
            paramObject.put(key,parameterMap.get(key));
        }
        String remoteIP = IPUtils.getRealRemoteIP(httpRequest);
        StringBuffer url = httpRequest.getRequestURL();
        //如果请求的是静态资源，获取静态资源
        log.debug(url.toString()+"-->本次请求参数 ："+paramObject);
        int lastIndexOf = url.toString().lastIndexOf(".");
        String exten = null;
        if (lastIndexOf!=-1){
            exten = url.toString().substring(lastIndexOf);
            if (exten.equals(".html")){
                exten=null;
            }
        }
        if (lastIndexOf!=-1&&exten!=null){
            log.debug("本次请求静态资源");
        }else{
            Syslog syslog = new Syslog();
            syslog.setActualIp(remoteIP);
            syslog.setRequestUrl(url.toString());
            syslog.setCreateDate(new Date());
            syslog.setRequestParam(paramObject.toJSONString());
            Object currentUser = httpRequest.getSession().getAttribute("current_user");
            if (currentUser!=null){
                if (currentUser instanceof User){
                    User user = (User)currentUser ;
                    syslog.setUserId(user.getUserId());
                    syslog.setUserName(user.getUserName());
                }else if(currentUser instanceof Admin){
                    Admin user = (Admin)currentUser ;
                    syslog.setUserId(user.getAdminId());
                    syslog.setUserName(user.getAdminName());
                }
            }
            log.debug("即将要插入的访问日志 ："+syslog);
            logMapper.insert(syslog);
        }
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
        log.debug("SyslogFilter destroy...");
    }
}
